package cn.icanci.loopstack.pcc.test.metagroup;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.icanci.loopstack.pcc.model.MetaCell;
import cn.icanci.loopstack.pcc.model.ObjectMetaCellGroup;
import cn.icanci.loopstack.pcc.test.key.KeyEnum;
import cn.icanci.loopstack.pcc.test.order.Order;
import cn.icanci.loopstack.pcc.test.order.OrderObject;

/**
 * @author icanci(1205068)
 * @version Id: OrderObjectMetaCellGroup, v 0.1 2024/8/2 15:29 icanci Exp $
 */
public class OrderObjectMetaCellGroup extends ObjectMetaCellGroup<OrderObject> {

    /**
     * 返回元组聚合数据
     *
     * @return 返回元组聚合数据
     */
    @Override
    public OrderObject adapterCells() {
        try {
            List<MetaCell<?, ?, ?>> metaCells = getMetaCells();
            Map<String, ? extends MetaCell<?, ?, ?>> keyMap = metaCells.stream().collect(Collectors.toMap(MetaCell::uniqueKey, x -> x));

            // 此处实现必须手动转型
            Order couponOrder = (Order) keyMap.get(KeyEnum.COUPON.name()).getTarget();
            Order goodsOrder = (Order) keyMap.get(KeyEnum.GOODS.name()).getTarget();

            OrderObject orderObject = new OrderObject();
            orderObject.setCoupon(couponOrder);

            orderObject.setGoods(goodsOrder);

            return orderObject;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}